const Router = require('koa-router')
const router = new Router()
const passport = require('koa-passport')
const Profile = require('../../model/Profiles')
const profileValidator = require('../../validation/profile')
router.get('/test', async (ctx, next) => {
    ctx.status = 200
    ctx.body = 'profiles hello world'
})

router.get('/',
    passport.authenticate('jwt', { session: false }),
    async (ctx, next) => {
        // populate 代表跨表联查 user代表模型 ['username', 'avatar'] 代表查询的字段
        const profile = await Profile.find({ user: ctx.state.user.id }).populate('user', ['username', 'avatar'])
        if (profile.length > 0) {
            ctx.status = 200;
            ctx.body = profile
        } else {
            ctx.status = 404;
            ctx.body = {
                noprofile: "该用户没有任何信息"
            }
            return
        }
        await next()
    }
)

// 添加和编辑
router.post('/',
    passport.authenticate('jwt', { session: false }),
    async (ctx, next) => {
        const { errors, isValid } = profileValidator(ctx.request.body)
        if (!isValid) {
            ctx.status = 400;
            ctx.body = errors
            return
        }
        const profileFilelds = {}
        profileFilelds.user = ctx.state.user.id
        if (ctx.request.body.handle) profileFilelds.handle = ctx.request.body.handle
        if (ctx.request.body.company) profileFilelds.company = ctx.request.body.company
        if (ctx.request.body.weisite) profileFilelds.weisite = ctx.request.body.weisite
        if (ctx.request.body.location) profileFilelds.location = ctx.request.body.location
        //skills 数据转换
        if (typeof ctx.request.body.skills !== undefined && ctx.request.body.skills !== null) {
            profileFilelds.skills = ctx.request.body.skills.split(',');
        }
        if (ctx.request.body.bio) profileFilelds.bio = ctx.request.body.bio

        if (ctx.request.body.githubusername) profileFilelds.githubusername = ctx.request.body.githubusername

        if (ctx.request.body.status) profileFilelds.status = ctx.request.body.status
        profileFilelds.social = {};
        if (ctx.request.body.wechart) profileFilelds.social.wechart = ctx.request.body.wechart
        if (ctx.request.body.QQ) profileFilelds.social.QQ = ctx.request.body.QQ
        if (ctx.request.body.tengxunkt) profileFilelds.social.tengxunkt = ctx.request.body.tengxunkt
        if (ctx.request.body.wangyikt) profileFilelds.social.wangyikt = ctx.request.body.wangyikt
        // 查询数据库 判断数据库有没有值如果有值的话代表是编辑
        // 没有值的话 是新建
        const profile = await Profile.find({ user: ctx.state.user.id })
        if (profile.length > 0) {
            // 代表是更新数据
            //{ user:ctx.state.user.id },{ $set:profileFilelds },{ new: true }
            // findByIdAndUpdate 更新数据方法
            // { user:ctx.state.user.id } 根据id 锁定用户
            // { $set: profileFilelds } 要更新的数据
            // { new: true } 是一个新数据
            const profileUpdate = await Profile.findOneAndUpdate({ user: ctx.state.user.id }, { $set: profileFilelds }, { new: true })
            ctx.status = 200
            ctx.body = profileUpdate
        } else {
            // 新建数据
            await new Profile(profileFilelds).save().then(profile => {
                ctx.status = 200
                ctx.body = profile
            })
        }
        await next()
    }
)
module.exports = router.routes()